#include "qtouchapplication.h"

#include <QPluginLoader>
#include <QDir>
#include <QMessageBox>
#include "../include/plugininterface.h"
#include "../lib/shared/widgetinterface.h"
#include "../lib/shared/qwidgetfactory.h"
#include "../lib/shared/qdriveplugininterface.h"
#include "../lib/shared/qproject.h"

int main(int argc, char *argv[])
{
    QTouchApplication a(argc, argv);

    QDir dir(QApplication::applicationDirPath()+"/plugins/");
    QFileInfoList list=dir.entryInfoList(QDir::Files);

    for(int i=0;i<list.size();i++)
    {
        QPluginLoader load(list.at(i).filePath());

        if(load.load())
        {
            PluginInterface *plugin=qobject_cast<PluginInterface*>(load.instance());
            if(plugin!=NULL)
            {
                a.addPlugin(plugin);
            }
        }
    }

    if(a.plugincount()==0)
    {
        return 0;
    }

    dir.setPath(QApplication::applicationDirPath()+"/drives/");
    list=dir.entryInfoList(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
    QString str=QApplication::applicationDirPath()+"/drives/";
    while(list.size()>0)
    {
        if(list.first().isDir())
        {
            QDir d(list.first().filePath());
            list+=d.entryInfoList(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
            list.removeFirst();
        }
        else
        {
            QPluginLoader load(list.first().filePath());

            if(load.load())
            {
                QDrivePluginInterface *plugin=qobject_cast<QDrivePluginInterface*>(load.instance());
                if(plugin!=NULL)
                {
                    QString s=list.first().filePath();
                    s.remove(0,str.size());
                    s.remove(s.size()-4,4);
                    QProject::project()->addDrivePlugin(s,plugin);
                }
            }
            list.removeFirst();
        }
    }


    a.showPage();

    return a.exec();
}
